package com.gba.mark.group.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gba.mark.group.entity.Group;
import com.gba.mark.group.service.IGroupService;
import com.gba.mark.group.service.Impl.GroupServiceImpl;
import com.gba.mark.user.auth.utils.AuthUtils;
import com.gba.mark.user.entity.user.User;
import com.gba.mark.user.service.user.IUserService;
import com.gba.mark.user.service.user.impl.UserServiceImpl;
import com.gba.mark.util.ResponseUtil;
import com.qigetech.utils.resultbundle.ResultBundle;
import com.qigetech.utils.resultbundle.ResultBundleBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.swing.plaf.IconUIResource;

@RestController
@RequestMapping("/group")



public class GroupController {


    @Autowired
    private ResultBundleBuilder resultBundleBuilder;

//    @Autowired
//    private GroupServiceImpl g;

    @Autowired
    private IGroupService groupService;

    @Autowired
    private IUserService userServiceImpl;

//    @GetMapping("/skip")
//    public ResultBundle<Boolean> skip(Authentication authentication){
//        String username = AuthUtils.getUsername(authentication);
//        User user = userServiceImpl.getOne(new QueryWrapper<User>().eq("name",username));
//        Integer userId = user.getId();
//        return resultBundleBuilder.bundle("skip",()->
//        groupServiceImpl.skip(userId));
//    }


    @RequestMapping("/saveGroup")
    public ResultBundle<Boolean> addGroup(Authentication authentication,
                                          @RequestParam String name,
                                          @RequestParam String language,
                                          @RequestParam Integer size//,
                                    //      @RequestParam Integer admin_id
                                          ) {
        String username = AuthUtils.getUsername(authentication);
        User user = userServiceImpl.getOne(new QueryWrapper<User>().eq("name",username));
    //    Integer userId = user;
//        group.setAdmin_id(userId);
        Integer admin_id = user.getId();
        return resultBundleBuilder.bundle("", () -> groupService.saveGroup(name, language, size, admin_id));
    }
//    public ResponseUtil<JSONObject> SetGroup(@RequestParam String name,
//                                             @RequestParam String language ,
//                                             @RequestParam int size,
//                                             @RequestParam int admin_id ) {
//        return new ResponseUtil<JSONObject>(HttpStatus.OK,
//                groupService.saveGroup(name, language, size, admin_id));
//   }

    @DeleteMapping ("/removeGroup")
    public ResultBundle<Boolean> removeGroup(//Authentication authentication,
                                          @RequestParam String name//,
                                          //@RequestParam String language,
                                         // @RequestParam Integer size
    ) {
      //  String username = AuthUtils.getUsername(authentication);
      //  User user = userServiceImpl.getOne(new QueryWrapper<User>().eq("name",username));
      //  Integer admin_id = user.getId();
        return resultBundleBuilder.bundle("", () -> groupService.removeGroup(name
               // ,  language, size, admin_id
        ));
    }

    @GetMapping("/searchGroup")
    public ResultBundle<Boolean> searchGroup(@RequestParam String name){
        return resultBundleBuilder.bundle("", () -> groupService.searchGroup(name));
    }
}
